import_failure_hint = """\
[yellow]failed during importing\n\t[bold]%s[/bold]
[cyan]you can try:\n\t[bold]& pip install %s[/bold]"""


def show_import_hint(module_name, package_name=None):  # TODO: allow multi packages
    from rich import print
    print(import_failure_hint % (module_name, package_name or module_name))


def try_import(module_name, package_name=None):
    try:
        exec(f"import {module_name}")
        return eval(module_name)
    except ImportError as err:
        show_import_hint(module_name, package_name)


def double_import(module_name):
    try:
        exec(f"import {module_name}")
        return eval(module_name)
    except ModuleNotFoundError:
        exec(f"from . import {module_name}")
        return eval(module_name)


def double_from_import(parent_name, child_name):
    if child_name in ".*":
        return NotImplementedError("under construction")  # TODO
    try:
        exec(f"from {parent_name} import {child_name}")
        return eval(child_name)
    except ModuleNotFoundError:
        exec(f"from .{parent_name} import {child_name}")
        return eval(child_name)
